import com.alibaba.fastjson.JSON;
import com.vichat.gps.entity.LocationData;
import com.vichat.user.vo.RequestParamsVO;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ranjx on 2018/6/25.
 */
public class ApiGpsHttpPost {

    private static Log logger = LogFactory.getLog(ApiGpsHttpPost.class);
    private static String defaultUrl = "http://localhost:8888/gpsApi/";
    public static void main(String[] args) {
        //上报定位信息
        uploadGps();
        //获取所有人员当天最新定位信息
        getUserLocations();
        //获取人员当天定位信息
        //getLocationByUid();
        //获取人员轨迹信息
        //getLocusByUid();
    }

    /**
     * 上传定位信息
     */
    public static void uploadGps(){
        String url = defaultUrl + "uploadGps";
        List<LocationData> locationDatas = new ArrayList<>();
        LocationData data1 = new LocationData();
        data1.setUid(300000002l);
        data1.setLongitude("121.47");
        data1.setLatitude("31.23");
        data1.setAddress("");
        data1.setLocType(1);
        data1.setLocTime(1521711814l);
        data1.setSumtime(0l);
        locationDatas.add(data1);
        LocationData data2 = new LocationData();
        data2.setUid(300000002l);
        data2.setLongitude("121.47");
        data2.setLatitude("31.23");
        data2.setAddress("");
        data2.setLocType(1);
        data2.setLocTime(1521711814l);
        data2.setSumtime(0l);
        locationDatas.add(data2);
        String json = JSON.toJSONString(locationDatas);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
    }

    /**
     * 获取所有人员当天最新定位信息
     */
    public static void getUserLocations(){
        String url = defaultUrl + "getUserLocations";
        RequestParamsVO paramsVO = new RequestParamsVO();
        paramsVO.setUid(300000961);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
    }

    /**
     * 获取人员当天定位信息
     */
    public static void getLocationByUid(){
        String url = defaultUrl + "getLocationByUid";
        RequestParamsVO paramsVO = new RequestParamsVO();
        paramsVO.setUid(300000475l);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
    }

    /**
     * 获取人员轨迹信息
     */
    public static void getLocusByUid(){
        String url = defaultUrl + "getLocusByUid";
        RequestParamsVO paramsVO = new RequestParamsVO();
        paramsVO.setUid(300000475l);
        paramsVO.setDate("");
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
    }

    /**
     * post 方法
     * @param url
     * @param json
     * @return
     */
    private static String post(String url, String json) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        String responseBody = null;
        try {
            // 建立一个NameValuePair数组，用于存储欲传送的参数
            method.addHeader("Content-type", "application/json; charset=utf-8");
            method.setHeader("Accept", "application/json");
            method.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            long startTime = System.currentTimeMillis();
            HttpResponse response = httpClient.execute(method);
            long endTime = System.currentTimeMillis();
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            logger.info("调用API 花费时间(单位：毫秒)：" + (endTime - startTime));
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed:" + response.getStatusLine());
            }
            // Read the response body
            responseBody = EntityUtils.toString(response.getEntity());
            logger.info("输出参数：" + responseBody);
        } catch (IOException e) {
            // 网络错误
            e.printStackTrace();
        } finally {
            return responseBody;
        }
    }
}
